package com.hao.demo.zoo.server.service.grpc;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hao.demo.zoo.entity.DepotsOperateLogEntity;
import com.hao.demo.zoo.protobuf.api.OperateLogRequest;
import com.hao.demo.zoo.protobuf.api.OperateLogResponse;
import com.hao.demo.zoo.protobuf.api.OperateLogResponseList;
import com.hao.demo.zoo.protobuf.api.OperateLogServiceGrpc;
import com.hao.demo.zoo.server.service.DepotsOperateLogService;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description: TODO
 * @Author Hao
 * @Date 2021/6/4 14:46
 * @Version 2.0
 */
@GrpcService
@Slf4j
public class DepotsOperateLogGrpcService extends OperateLogServiceGrpc.OperateLogServiceImplBase {
    @Autowired
    private DepotsOperateLogService depotsOperateLogService;

    @Override
    public void getByBusinessId(OperateLogRequest request, StreamObserver<OperateLogResponseList> observer){
        long s = System.currentTimeMillis();
        log.debug("getByBusinessId 开始时间：" + s);

        List<DepotsOperateLogEntity> lists = depotsOperateLogService.getBaseMapper().selectList(
                Wrappers.lambdaQuery(DepotsOperateLogEntity.class)
                        .eq(DepotsOperateLogEntity::getBusinessId, request.getBusinessId())
        );

        OperateLogResponseList.Builder builderList = OperateLogResponseList.newBuilder();
        // 创建响应对象
        for(DepotsOperateLogEntity entity: lists){
            OperateLogResponse.Builder builder = OperateLogResponse.newBuilder();
            BeanUtils.copyProperties(entity, builder);

            builderList.addResponse(builder);
        }

        // 返回响应
        observer.onNext(builderList.build());
        observer.onCompleted();

        long e = System.currentTimeMillis();
        log.debug("getByBusinessId 结束时间：" + e);
        log.debug("getByBusinessId 耗时时间：" + (e - s));
    }
}
